<template>
  <template v-if="loaded">
    <ProForm
      ref="formRef"
      :model="formState"
      :items="items"
      :disabled="readonly"
      :onValuesChange="onValuesChange"
      :formColLayout="formColLayout()"
      :requiredMark="!readonly"
      class="pro-form-modal"
      v-bind="formProps"
    >
    </ProForm>
    <FooterButtons
      :formState="formState"
      :formRef="formRef"
      v-model:readonly="readonly"
      :serviceApi="updateVolumeConfig"
      @refresh="refresh"
      :fastConfig="fastConfig"
    />
  </template>
</template>

<script setup lang="tsx">
import { computed, nextTick, ref, shallowRef } from 'vue';
import { ProForm, ProFormProps } from '@/components/ProForm';
import { ItemProps } from '@/components/ProForm/interface';
import { getVolumeConfig, updateVolumeConfig } from '@/api/device';
import FooterButtons from './FooterButtons.vue';

defineOptions({
  name: 'Volume',
});

interface Props {
  pageData: any;
  readonly?: any;
  fastConfig?: boolean;
}
const props = defineProps<Props>();
const loaded = ref(false);
const readonly = ref(props.readonly ?? true);
const formRef = shallowRef();
const formState = ref<Record<string, any>>({});
const column = 4;
const formColLayout = () => {
  const num = 24 / column;
  return { xs: num, sm: num, md: num, lg: num };
};
const formProps = computed<ProFormProps>(() => {
  return {
    labelCol: { span: 10 },
    wrapperCol: { span: 20 },
  };
});
const onValuesChange = (
  values: Record<string, any>,
  state: Record<string, any>,
) => {
  formState.value = {
    ...formState.value,
    ...state,
  };
};
const refresh = () => {
  loaded.value = false;
  nextTick(() => {
    loaded.value = true;
  });
};
const itemObj = (
  label: string,
  type: any,
  field: string,
  required: boolean = false,
  props?: any,
  layout?: any,
  rules: any = [],
): ItemProps => {
  return {
    label,
    type,
    field,
    props: {
      placeholder: `请${type == 'input' ? '输入' : '选择'}${label}`,
      allowClear: false,
      ...props,
    },
    rules: [
      {
        required,
        message: `请${type == 'input' ? '输入' : '选择'}${label}`,
        trigger: 'blur',
      },
      ...rules,
    ],
    ...layout,
  };
};
const itemRender = (title: string): ItemProps => {
  return {
    label: { title },
    type: 'input',
    field: 'h5',
    formItemRender() {
      return <h5 class='h5'>{title}</h5>;
    },
  };
};
let warnAudioList: any = [];

const items = computed<ProFormProps['items']>(() => [
  itemRender('参数配置'),
  itemObj('蓝色预警音量', 'input', 'warnBlueVolume', true),
  itemObj('黄色预警音量', 'input', 'warnYellowVolume', true),
  itemObj('蓝色预警音频', 'select', 'warnBlueAudio', false, {
    options: warnAudioList,
  }),

  itemObj('黄色预警音频', 'select', 'warnYellowAudio', false, {
    options: warnAudioList,
  }),
  itemObj('橙色预警音量', 'input', 'warnOrangeVolume', true),
  itemObj('红色预警音量', 'input', 'warnRedVolume', true),
  itemObj('橙色预警音频', 'select', 'warnOrangeAudio', false, {
    options: warnAudioList,
  }),

  itemObj('红色预警音频', 'select', 'warnRedAudio', false, {
    options: warnAudioList,
  }),
  itemObj('速报播报音量', 'input', 'reportVolume', true),
  itemObj('气象播放音量', 'input', 'weatherWarnVolume', true),
  itemObj('', null, '', false),
  itemObj('', null, '', false),
  itemObj('视频播放音量', 'input', 'videoVolume', true),
  itemObj('消音音量', 'input', 'silenceVolume', true),
  itemObj('', null, '', false),
  itemObj('', null, '', false),
  itemObj('自检音量', 'input', 'checkVolume', true),
  itemObj('广播音量', 'input', 'brocastVolume', true),
]);

props.pageData.volumeConfigsJson
  ? (() => {
      formState.value = JSON.parse(props.pageData.volumeConfigsJson);
      warnAudioList = [
        { value: '', label: '无' },
        { value: 'warning2.mp3', label: 'warning2.mp3' },
        { value: 'warning.wav', label: 'warning.wav' },
      ];
      loaded.value = true;
    })()
  : getVolumeConfig(props.pageData.code).then((res: any) => {
      formState.value = res.data;
      formState.value.code = props.pageData.code;
      warnAudioList = formState.value.warnAudioList?.map((item: any) => ({
        value: item,
        label: item,
      }));
      loaded.value = true;
    });

defineExpose({
  async save() {
    const data = await formRef.value.validate();
    props.pageData.volumeConfigsJson = JSON.stringify(data);
  },
});
</script>
